package nl.fonfolio.view {
	
	import nl.fonfolio.core.FonfolioEvent;	
	import nl.fonfolio.controller.Controller;	
	import nl.fonfolio.model.Model;	

	import flash.display.*;

	import caurina.transitions.Tweener;

	import flash.events.MouseEvent;
	import flash.events.Event;

	import nl.fonfolio.core.Fonfolio;	
	
	/**
	 * @author Paulus de boskabouter
	 */
	public class Panel extends Sprite {
		public var fonfolio : Fonfolio;
		public var background : MovieClip;
		public var widgetcontainer : Sprite = new Sprite();
		public var close_btn : MovieClip;	
		private var model : Model;
		private var controller : Controller;
		public var opened : Boolean;//false=closed,true=opened
		public var widget : Object;
	

		public function Panel(fonfolio : Fonfolio, model : Model, controller : Controller) {
			this.controller = controller;
			this.model = model;
			this.fonfolio = fonfolio;
			this.widgetcontainer = new Sprite();
			
			this.addChild(this.widgetcontainer);
			
			this.fonfolio.stage.addEventListener(Event.RESIZE, this.onResizeStage);
			//this.model.addEventListener(FonfolioEvent.CONTROL_TYPE, this.openPanelHandler);
			this.close_btn.addEventListener(MouseEvent.CLICK, this.close);

			
			this.x = this.fonfolio.stage.stageWidth;
		}
		


		private function onResizeStage(event : Event) : void {
			this.resize();
		}

		private function resize() : void {  
			this.background.height = this.fonfolio.stage.stageHeight;
			this.widgetcontainer.y = this.background.height / 2 - this.widgetcontainer.height / 2 ;
			if (this.opened == true) {
				this.x = this.fonfolio.stage.stageWidth - this.background.width;
			}
			else {
				trace("panel resize, false, set panel to stagewith");
				this.x = this.fonfolio.stage.stageWidth;
			}
		}

		public function removePanel():void{
			this.parent.removeChild(this);	
		}

		public function close(event : Event=null) : void {
			this.opened = false;
			
			Tweener.addTween(this, {x:this.fonfolio.stage.stageWidth, time:0.5, transition:"easeInQuart", onComplete:removePanel});
		}
		

		public function reopen() : void {
			Tweener.addTween(this, {x:this.fonfolio.stage.stageWidth, time:0.8, transition:"easeInQuart",onComplete:open});
		}
		
		private function remove() : void {
			this.fonfolio.removeChild(this);
			//this.fonfolio.panel = null;
		} 

		public function open():void{
			Tweener.addTween(this, {x:this.fonfolio.stage.stageWidth-this.background.width, time:0.5, transition:"easeOutQuart", onComplete:panelLoaded});
		}

		
		public function panelLoaded():void{
			//
			this.opened = true;
		}

		public function addWidget(widget : Sprite) : void {
			widget.y = this.widgetcontainer.height;

			this.widgetcontainer.addChild(widget);
			this.resize();
		}
	}
}
